<?php

////////////////////////////////////////////////////////////////////////////////////////////////////
//
// phpBBnews
// =========
//
// Copyright (C) 2007 mgsninja
// Some functions are taken from phpBB 2 and are copyright (C) 2001 The phpBB Group
// The changes of these are copyright (C) 2007 mgsninja
//
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
////////////////////////////////////////////////////////////////////////////////////////////////////


include('phpbbnews/config.php');
include('phpbbnews/template.php');
include('phpbbnews/BBcode.php');
include('phpbbnews/functions.php');

/**
 * Connect to database
 */
@mysql_connect($pbn['DB_Host'], $pbn['DB_UserName'], $pbn['DB_Password']) or die(mysql_error());
@mysql_select_db($pbn['DB_Name']) or die(mysql_error());

/**
 * Print news header
 */
echo $pbn['Layout_Header'];
/**
 * Get news for database
 */
$pbn['SQL_GetNews'] =
	'SELECT
		topic_id, topic_title, topic_poster, topic_time, topic_replies
	FROM
		'.$pbn['DB_TablePrefix'].'topics
	WHERE
		forum_id = '.$pbn['News_Forum'].'
	ORDER BY
		topic_id DESC
	LIMIT '.$pbn['News_Limit'];
$pbn['Query_GetNews'] = @mysql_query($pbn['SQL_GetNews']);
$news = array();
while($pbn['Result_GetNews'] = @mysql_fetch_array($pbn['Query_GetNews']))
{
	$pbn['Result_GetNews']['topic_id'];
	$pbn['Result_GetNews']['topic_title'];
	$pbn['Result_GetNews']['topic_poster'];
	$pbn['Result_GetNews']['topic_time'];
	$pbn['Result_GetNews']['topic_replies'];
	$pbn['Result_GetNews']['topic_time'] = $pbn['Result_GetNews']['topic_time'] + $pbn['News_LocalTime'];
	$pbn['Result_GetNews']['topic_time'] = date($pbn['News_Date'], $pbn['Result_GetNews']['topic_time']);

	/**
	 * Is there 0, 1 or more comments
	 */
	if($pbn['Result_GetNews']['topic_replies'] == 1)
	{
		$pbn['Comment'] = '(1) '.$pbn['News_Comment'];
	}
	else
	{
		$pbn['Comment'] = '('.$pbn['Result_GetNews']['topic_replies'].') '.$pbn['News_Comments'];
	}

	/**
	 * Get username
	 */
	$pbn['SQL_GetUsername'] =
	'SELECT
		username
	FROM
		'.$pbn['DB_TablePrefix'].'users
	WHERE
		user_id = '.$pbn['Result_GetNews']['topic_poster'];
	$pbn['Query_GetUsername'] = @mysql_query($pbn['SQL_GetUsername']);
	$pbn['Result_GetUsername'] = @mysql_fetch_array($pbn['Query_GetUsername']);
	$pbn['Result_GetUsername']['username'];

	/**
	 * Get post id
	 */
	$pbn['SQL_GetPostId'] =
	'SELECT
		post_id
	FROM
		'.$pbn['DB_TablePrefix'].'posts
	WHERE
		topic_id = '.$pbn['Result_GetNews']['topic_id'].'
	ORDER BY
		post_id ASC
	LIMIT 1';
	$pbn['Query_GetPostId'] = @mysql_query($pbn['SQL_GetPostId']);
	$pbn['Result_GetPostId'] = @mysql_fetch_array($pbn['Query_GetPostId']);
	$pbn['Result_GetPostId']['post_id'];

	/**
	 * Get post text
	 */
	$pbn['SQL_GetText'] =
	'SELECT
		post_text, bbcode_uid
	FROM
		'.$pbn['DB_TablePrefix'].'posts_text
	WHERE
		post_id = '.$pbn['Result_GetPostId']['post_id'];
	$pbn['Query_GetText'] = @mysql_query($pbn['SQL_GetText']);
	$pbn['Result_GetText'] = @mysql_fetch_array($pbn['Query_GetText']);
	$pbn['Result_GetText']['post_text'] = bbencode($pbn['Result_GetText']['post_text'], $pbn['Result_GetText']['bbcode_uid']);
	/**
	 * Turn smilies into images
	 */
	if($pbn['Smilies_code'] == 'On')
	{
		$pbn['Result_GetText']['post_text'] = smilies($pbn['Result_GetText']['post_text']);
	}
	else
	{
		$pbn['Result_GetText']['post_text'] = smiliesRemove($pbn['Result_GetText']['post_text']);
	}
	#$pbn['Result_GetText']['post_text'] = preg_replace("/\[.*?:".$pbn['Result_GetText']['bbcode_uid'].":?.*?\]/si", '', $pbn['Result_GetText']['post_text']);
	#$pbn['Result_GetText']['post_text'] = preg_replace('/\[url\]|\[\/url\]/si', '', $pbn['Result_GetText']['post_text']);
	/**
	 * Limit the lengt of the post text
	 */
	if($pbn['News_Length'] != 0)
	{
		$pbn['Result_GetText']['post_text'] = ( strlen($pbn['Result_GetText']['post_text']) > $pbn['News_Length'] ) ? substr($pbn['Result_GetText']['post_text'], 0, $pbn['News_Length']) . ' ...' : $pbn['Result_GetText']['post_text'];
	}
	/**
	 * Make "\n" into <br />
	 */
	$pbn['Result_GetText']['post_text'] = str_replace("\n", "\n<br />\n", $pbn['Result_GetText']['post_text']);

	/**
	 * Run template to input the data
	 */
	$pbn['Layout_BodyPrint'] = $pbn['Layout_Body'];
	$pbn['Layout_BodyPrint'] = str_replace('{PATH}',         $pbn['General_phpBB2Path'],             $pbn['Layout_BodyPrint']);
	$pbn['Layout_BodyPrint'] = str_replace('{TOPIC_ID}',     $pbn['Result_GetNews']['topic_id'],     $pbn['Layout_BodyPrint']);
	$pbn['Layout_BodyPrint'] = str_replace('{TOPIC_TITLE}',  $pbn['Result_GetNews']['topic_title'],  $pbn['Layout_BodyPrint']);
	$pbn['Layout_BodyPrint'] = str_replace('{TOPIC_POSTER}', $pbn['Result_GetNews']['topic_poster'], $pbn['Layout_BodyPrint']);
	$pbn['Layout_BodyPrint'] = str_replace('{USERNAME}',     $pbn['Result_GetUsername']['username'], $pbn['Layout_BodyPrint']);
	$pbn['Layout_BodyPrint'] = str_replace('{TOPIC_TIME}',   $pbn['Result_GetNews']['topic_time'],   $pbn['Layout_BodyPrint']);
	$pbn['Layout_BodyPrint'] = str_replace('{POST_TEXT}',    $pbn['Result_GetText']['post_text'],     $pbn['Layout_BodyPrint']);
	$pbn['Layout_BodyPrint'] = str_replace('{COMMENTS}',     $pbn['Comment'],                        $pbn['Layout_BodyPrint']);

	/**
	 * Print news body
	 */
	echo $pbn['Layout_BodyPrint'];
}

/**
 * Print news footer
 */
echo $pbn['Layout_Footer'];

unset($pbn);

?>